//
//  CycleScrollView.m
//  06-UI
//
//  Created by Snow on 16/7/21.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "CycleScrollView.h"

@interface CycleScrollView ()<UIScrollViewDelegate>


@end
@implementation CycleScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initImagePageWithFrame:frame];
        [self initWithScrollView];
    }
    return self;
}
- (void)initImagePageWithFrame:(CGRect)frame
{
    _pageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width / 3, 20)];
    _pageControll.numberOfPages = 5;
    _pageControll.center = CGPointMake(frame.size.width / 2, frame.size.height - 20 / 2);
    [self addSubview:_pageControll];
    [_pageControll addTarget:self action:@selector(pageControllChange:) forControlEvents:UIControlEventValueChanged];
    [self initWithScrollView];
    
}
- (void)pageControllChange:(UIPageControl *)page
{
    [_scrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}

- (void)initWithScrollView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
}
- (void)setImagesWithArray:(NSArray *)array
{
    NSArray *imagesArray = [self handleArrayWithArray:array];
    self.imageArray = [NSArray arrayWithArray:imagesArray];
    [self createAllImagesViewsWithImageArray:imagesArray];
    _pageControll.numberOfPages = array.count;
    _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    _scrollView.contentSize = CGSizeMake(imagesArray.count * self.bounds.size.width, 0);
}
- (void)createAllImagesViewsWithImageArray:(NSArray *)imagesArray
{
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_scrollView addSubview:imageView];
    }
}
- (NSArray *)handleArrayWithArray:(NSArray *)array
{
    //判断类型(不知道别人使用你的类的时候传进来的类型是什么)
    id firstObject = [array firstObject];
    //把最终的图片数组
    NSMutableArray *imagesArray = [NSMutableArray array];
    //判断对象是不是某种类型 isKindOfclass
    if ([firstObject isKindOfClass:[NSString class]]) {
        //把字符串数组 转成图片数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        //直接使用数组创建ImageView
        [imagesArray setArray:array];
    }
    //第一张图
    UIImage *first = [imagesArray firstObject];
    //最后一张图
    UIImage *last = [imagesArray lastObject];
    //    下标为0的位置放最后一张图
    [imagesArray insertObject:last atIndex:0];
    //    在数组最后添加第一张图
    [imagesArray addObject:first];
    return [imagesArray copy];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _pageControll.currentPage = page - 1;
    if (page == 0) {
        //改变滚动视图的偏移量
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imageArray.count - 2), 0);
        _pageControll.currentPage = _imageArray.count - 3;
    } else if (page == _imageArray.count - 1) {
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _pageControll.currentPage = 0;
    }
}

@end
